package pokerantani.v2.model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import pokerantani.v2.HandDetector;

public class Hand {
	/**
	 * List of players sitting at the table
	 */
	public List<Player> players = new ArrayList<Player>();
	
	/**
	 * Small blind bet
	 */
	public float smallBlind;
	
	/**
	 * Big blind bet
	 */
	public float bigBlind;
	
	/**
	 * Button (or dealer)
	 */
	public Player buttonPlayer;
	
	/**
	 * Small blind player
	 */
	public Player smallBlindPlayer;
	
	/**
	 * Big blind player
	 */
	public Player bigBlindPlayer;
	
	/**
	 * The three cards revealed at the "flop"
	 */
	public List<Card> flopCards = new ArrayList<Card>();
	
	/**
	 * The card revealed at the "turn"
	 */
	public Card turnCard;
	
	/**
	 * The card revealed at the "river"
	 */
	public Card riverCard;
	
	/**
	 * List of actions for each game phase
	 */
	public Map<GamePhase, List<Action>> actions = new HashMap<GamePhase, List<Action>>();
	{
		actions.put(GamePhase.INTRO, new ArrayList<Action>());
		actions.put(GamePhase.PREFLOP, new ArrayList<Action>());
		actions.put(GamePhase.FLOP, new ArrayList<Action>());
		actions.put(GamePhase.TURN, new ArrayList<Action>());
		actions.put(GamePhase.RIVER, new ArrayList<Action>());
		actions.put(GamePhase.SHOWDOWN, new ArrayList<Action>());
		actions.put(GamePhase.SUMMARY, new ArrayList<Action>());
	}
	
	public String toString() {
		StringBuilder s = new StringBuilder();
		String nl = "\n";
		s.append("SB=" + smallBlind + " BB=" + bigBlind + nl);
		s.append("Button is " + buttonPlayer + nl);
		for(Player player : players)
			s.append("Player " + player + " has " + player.chips + " chips" + nl);
		s.append(players.get(0) + " has cards: " + players.get(0).cards + nl);
		for(Action action : actions.get(GamePhase.PREFLOP)) s.append("" + action + nl);
		s.append("FLOP: " + flopCards + nl);
		for(Action action : actions.get(GamePhase.FLOP)) s.append("" + action + nl);
		s.append("TURN: [" + turnCard + "]" + nl);
		for(Action action : actions.get(GamePhase.TURN)) s.append("" + action + nl);
		s.append("RIVER: [" + riverCard + "]" + nl);
		for(Action action : actions.get(GamePhase.RIVER)) s.append("" + action + nl);
		for(Player player : players)
			if(!player.cards.isEmpty())
				s.append(player + " has cards " + player.cards + " -> " + detectPlayerHand(player) + nl);
		return s.toString();
	}
	
	private String detectPlayerHand(Player player) {
		if(player.cards.isEmpty())
			return "UNKNOWN CARDS";
		List<Card> x = new ArrayList<Card>();
		x.addAll(flopCards);
		x.add(turnCard);
		x.add(riverCard);
		x.addAll(player.cards);
		return HandDetector.handToString(HandDetector.detectHand(x));
	}
}
